var mysql = require('mysql')
/*select * from `user`.uc_user where cell='15157265901';
select *from  credit.credit_order where client_no = '82014858095504752640'
SELECT * FROM riskgateway.t_user_product_credit  WHERE CLIENT_NO = '82014858095504752640';*/

/*var pool = mysql.createPool({
    host:'10.100.2.12',
    user:'root',
    password:'Wo4heike',
    database:'yingxinxin-qa',
    port:3306
})*/
/*服务器IP：10.100.5.96
端口：3306
用户名：root
密码：yylc8888*/


var qb = {
     qblimitcore_data :{
        host : '10.100.5.96',
        user :'root',
        password : 'yylc8888',
        port : 3306,
        database:'limitcore'
    },

    qbriskgateway_data  :{
        host : '10.200.1.38',
        user :'kaifa',
        password : 'kaifa',
        port : 4001,
        database:'riskgateway'
    },
   qbriskdata_data : {
        host : '10.200.1.38',
        user :'kaifa',
        password : 'kaifa',
        port : 4001,
        database:'riskdata'
    },
     qbuser_data : {
        host : '10.100.20.173',
        user :'yinghuo',
        password : '123456',
        port : 3306,
        database:'user'
    },
   qbcredit_data:{
        host : '10.100.20.173',
        user :'yinghuo',
        password : '123456',
        port : 3306,
        database:'credit'
    },
     loanmarket_data : {
        host:'10.100.2.12',
        user:'root',
        password:'Wo4heike',
        database:'loan-market-test',
        port:3306
    }
}
var qe = {
    qblimitcore_data :{
        host : '10.200.1.51',
        user :'ceshi',
        password : 'ceshi7890',
        port : 3306,
        database:'limitcore'
    },

    qbriskgateway_data  :{
        host : '10.200.1.71',
        user :'ceshi3309',
        password : 'ceshi7890',
        port : 3309,
        database:'riskgateway'
    },
    qbriskdata_data : {
        host : '10.200.1.71',
        user :'ceshi3309',
        password : 'ceshi7890',
        port : 3309,
        database:'riskdata'
    },
    qbuser_data : {
        host : '10.200.1.71',
        user :'ceshi3309',
        password : 'ceshi7890',
        port : 3309,
        database:'user'
    },
    qbcredit_data:{
        host : '10.200.1.71',
        user :'ceshi3309',
        password : 'ceshi7890',
        port : 3309,
        database:'credit'
    },
    loanmarket_data : {
        host:'10.100.2.12',
        user:'root',
        password:'Wo4heike',
        database:'loan-market-qe',
        port:3306
    }
}

function getEVN() {
    return qe
}


var qbriskgateway  = mysql.createPool(
    getEVN().qbriskgateway_data
)
var qbuser  = mysql.createPool(
    getEVN().qbuser_data
)
var qbcredit  = mysql.createPool(
    getEVN().qbcredit_data
)
var qbriskdata =mysql.createPool(getEVN().qbriskdata_data)
var loanmarket = mysql.createPool(getEVN().loanmarket_data)
var qblimitcore = mysql.createPool(getEVN().qblimitcore_data)
/*class Person{
    constructor(){

    }
}*/


var query = function (pool,sql,sqlparam,callback) {
    pool.getConnection(function (err,conn) {
        if (err){
            callback(null,err,null)
        } else {
            conn.query(sql,sqlparam,function (qerr,vals,fields) {
                conn.release();
                callback(qerr,vals,fields)
            })
        }
    })
}
var cell = '18512527459'
var cert_no = '362302198908236013'
var blacklist = 13 //0黑名单  1 白名单
var app_code =23 //0 有钱 2 福利版  1 盈花花
var clientno =new Promise(
    (resolve, reject) => {
        query(qbuser,'select * from `user`.uc_user where app_code = ? and cell=? ',[app_code,cell],function (qerr,vals,fields) {
            console.log(vals[0].client_no)
            resolve(vals[0])
        })
    }
)

function deleteData(){
    let cell = '18512527459'
    let cert_no = '362302198908236013'
    let blacklist = 13  //0黑名单  1 白名单
    let app_code =0 //0 有钱 2 福利版  1 盈花花
    let clientno =new Promise(
        (resolve, reject) => {
            query(qbuser,'select * from `user`.uc_user where app_code = ? and cell=? ',[app_code,cell],function (qerr,vals,fields) {
                console.log(vals[0].client_no)
                resolve(vals[0])
            })
        }
    )
    clientno.then(data => {
        if (blacklist === 0){//加黑名单
            let  sql = "INSERT INTO `riskdata`.`t_data_risk_blacklist` ( `MOBILE`, `NAME`, `ID_CARD`, `DATA_SOURCE`, `BIZ_CHANNEL`, `STATE`, `GMT_CREATED`, `GMT_MODIFIED`) " +
                "VALUES ( ?, ?, ?, 'YINGYING', 'P2P', '1', '2017-09-25 18:40:40', '2017-09-25 20:43:45');"//手机号，姓名，身份证
            let  params = [data.cell,data.real_name,cert_no]
            query(qbriskdata,sql,params,function (qerr,vals,fields) {
                console.log("qbriskdata,vals",Array.from(vals))
            })
        } else if(blacklist === 1) {//加白名单
            let sql = "INSERT INTO `riskgateway`.`t_whitelist` (`CLIENT_NO`, `PRODUCT_CODE`, `CERT_NO`, `REAL_NAME`, `MOBILE`, `ROLE_LEVEL`, `CREDIT_AMOUNT`, `STATE`, `CREATE_TIME`, `UPDATE_TIME`) " +
                "VALUES (?, 'P1075', '320821198612201986', '王腊梅', '', '', '10000.00', '1', '2018-01-30 14:33:43', '2018-01-30 14:33:43');"//clientno,身份证，名字
            let params = [data.client_no,cert_no,data.real_name]
            query(qbriskgateway,sql,params,function (qerr,vals,fields) {
                console.log("qbriskgateway,vals",Array.from(vals))
            })
        }
        query(qbriskgateway,'delete from t_user_product_credit where CLIENT_NO = ?',data.client_no,function (qerr,vals,fields) {
            console.log("qbriskgateway,vals",Array.from(vals))
            console.log("qbriskgateway,qerr",qerr)
            console.log("qbriskgateway,fields",fields)
        })
        query(qbcredit,'delete from  credit.credit_order where client_no = ?',data.client_no,function (qerr,vals,fields) {
            console.log('qbcredit,vals:'+Array.from(vals))
            console.log('qbcredit,qerr:'+qerr)
            console.log('qbcredit,fields:'+fields)
        })
        query(qblimitcore,'delete from limitcore_credit_detail where relate_no=?',data.client_no,function (qerr,vals,fields) {
            console.log('qbcredit,vals:'+Array.from(vals))
            console.log('qbcredit,qerr:'+qerr)
            console.log('qbcredit,fields:'+fields)
        })
        query(loanmarket,'delete from `loan-market-test`.loanmarket_merchant_loan_order where user_id = ?',data.user_id,function (qerr,vals,fields) {
            console.log('loanmarket_merchant_loan_order,vals:'+Array.from(vals))
            console.log('loanmarket_merchant_loan_order,qerr:'+qerr)
            console.log('loanmarket_merchant_loan_order,fields:'+fields)
        })

    })
}
 clientno.then(data => {
     if (blacklist === 0){//加黑名单
         let  sql = "INSERT INTO `riskdata`.`t_data_risk_blacklist` ( `MOBILE`, `NAME`, `ID_CARD`, `DATA_SOURCE`, `BIZ_CHANNEL`, `STATE`, `GMT_CREATED`, `GMT_MODIFIED`) " +
               "VALUES ( ?, ?, ?, 'YINGYING', 'P2P', '1', '2017-09-25 18:40:40', '2017-09-25 20:43:45');"//手机号，姓名，身份证
         let  params = [data.cell,data.real_name,cert_no]
        query(qbriskdata,sql,params,function (qerr,vals,fields) {
           // console.log("qbriskdata,vals",Array.from(vals))
        })
     } else if(blacklist === 1) {//加白名单
        let sql = "INSERT INTO `riskgateway`.`t_whitelist` (`CLIENT_NO`, `PRODUCT_CODE`, `CERT_NO`, `REAL_NAME`, `MOBILE`, `ROLE_LEVEL`, `CREDIT_AMOUNT`, `STATE`, `CREATE_TIME`, `UPDATE_TIME`) " +
            "VALUES (?, 'P1075', '320821198612201986', '王腊梅', '', '', '10000.00', '1', '2018-01-30 14:33:43', '2018-01-30 14:33:43');"//clientno,身份证，名字
         let params = [data.client_no,cert_no,data.real_name]
         query(qbriskgateway,sql,params,function (qerr,vals,fields) {
            // console.log("qbriskgateway,vals",Array.from(vals))
         })
     }
     query(qbriskgateway,'delete from t_user_product_credit where CLIENT_NO = ?',data.client_no,function (qerr,vals,fields) {
         console.log("qbriskgateway,vals",Array.from(vals))
         console.log("qbriskgateway,qerr",qerr)
         console.log("qbriskgateway,fields",fields)
     })
     query(qbcredit,'delete from  credit.credit_order where client_no = ?',data.client_no,function (qerr,vals,fields) {
         console.log('qbcredit,vals:'+Array.from(vals))
         console.log('qbcredit,qerr:'+qerr)
         console.log('qbcredit,fields:'+fields)
     })
     query(qblimitcore,'delete from limitcore_credit_detail where relate_no=?',data.client_no,function (qerr,vals,fields) {
         console.log('qbcredit,vals:'+Array.from(vals))
         console.log('qbcredit,qerr:'+qerr)
         console.log('qbcredit,fields:'+fields)
     })



     query(loanmarket,'delete from `loan-market-test`.loanmarket_merchant_loan_order where user_id = ?',data.user_id,function (qerr,vals,fields) {
         console.log('loanmarket_merchant_loan_order,vals:'+Array.from(vals))
         console.log('loanmarket_merchant_loan_order,qerr:'+qerr)
         console.log('loanmarket_merchant_loan_order,fields:'+fields)
     })
     query(loanmarket,'delete from `loan-market-test`.loanmarket_merchant_loan_apply_record where client_id = ?',data.client_id,function (qerr,vals,fields) {
     //    console.log('loanmarket_merchant_loan_order,vals:'+Array.from(vals))
         console.log('loanmarket_merchant_loan_order,qerr:'+qerr)
         console.log('loanmarket_merchant_loan_order,fields:'+fields)
     })

  query(loanmarket,'delete from `loan-market-test`.loanmarket_merchant_loan_pre_audit_record where user_id = ?',data.user_id,function (qerr,vals,fields) {
        // console.log('loanmarket_merchant_loan_order,vals:'+Array.from(vals))
         console.log('loanmarket_merchant_loan_order,qerr:'+qerr)
         console.log('loanmarket_merchant_loan_order,fields:'+fields)
     })
  query(loanmarket,'delete from `loan-market-test`.loanmarket_merchant_loan_repay_record where user_id = ?',data.user_id,function (qerr,vals,fields) {
        // console.log('loanmarket_merchant_loan_order,vals:'+Array.from(vals))
         console.log('loanmarket_merchant_loan_order,qerr:'+qerr)
         console.log('loanmarket_merchant_loan_order,fields:'+fields)
     })
  query(loanmarket,'delete from `loan-market-test`.loanmarket_merchant_product_user_register where user_id = ?',data.user_id,function (qerr,vals,fields) {
       //  console.log('loanmarket_merchant_loan_order,vals:'+Array.from(vals))
         console.log('loanmarket_merchant_loan_order,qerr:'+qerr)
         console.log('loanmarket_merchant_loan_order,fields:'+fields)
     })
  query(loanmarket,'delete from `loan-market-test`.loanmarket_merchant_user where user_id = ?',data.user_id,function (qerr,vals,fields) {
       //  console.log('loanmarket_merchant_loan_order,vals:'+Array.from(vals))
         console.log('loanmarket_merchant_loan_order,qerr:'+qerr)
         console.log('loanmarket_merchant_loan_order,fields:'+fields)
     })






        // client_no   user_id
 })




class Sql{
    constructor(){
        this.name = 'ccc'
        this.model = function () {
            return '555'
        }
    }
    query(pool, sql, sqlparam){
        return new Promise((resolve,reject)=>{
            pool.getConnection((err, connect) => {
                if (err) {
                    reject(err)
                }else{
                    connect.query(sql, sqlparam, (qerr, vals, fields)=> {
                        connect.release();
                        resolve(qerr, vals, fields)
                    })
                }
            })
        })
    }
    async run(pool,sql, sqlparam){
        try{
            const clientUser = await query(pool, sql, sqlparam)
            const qbriskgateway = await query(pool, sql, clientUser)
            const qbcredit = await query(pool, sql, clientUser)
        }catch(error){
            throw error
        }
    }
}

module.exports=deleteData

